package com.TP.www.service.impl;

import com.TP.www.dao.impl.*;
import com.TP.www.po.Merchant;
import com.TP.www.po.Order;
import com.TP.www.po.Store;
import com.TP.www.service.OrderPort;

import java.util.ArrayList;
import java.util.List;

import static com.TP.www.constant.Constant.*;

/**
 * @author 邱彦祥
 * 订单功能实现类
 */
public class OrderImpl implements OrderPort {
    OrderDAO orderDAO = new OrderDAO();
    MerchantDAO merchantDAO = new MerchantDAO();
    StoreDAO storeDAO = new StoreDAO();
    /**
     * 打印订单
     * @param order 订单
     * @return 订单字符串
     */
    @Override
    public String printOrder(Order order){
        if(order.getState() == -1){
            return  "用户: " + order.getName() +
                    "商品名: " + order.getGoodName()  +
                    " 数量: " + order.getNums() +
                    " 单价: " + order.getPrice() + "元" +
                    " 优惠金额: " + order.getDiscount() + "元" +
                    " 总价: " + order.getTotalCost() + "元" +
                    " 用户已下单!";
        }else if (order.getState() == 0){
            return  "用户: " + order.getName() +
                    "商品名: " + order.getGoodName()  +
                    " 数量: " + order.getNums() +
                    " 单价: " + order.getPrice() + "元" +
                    " 优惠金额: " + order.getDiscount() + "元" +
                    " 总价: " + order.getTotalCost() + "元" +
                    " 商家已接单!";
        }else {
            return  "用户: " + order.getName() +
                    "商品名: " + order.getGoodName()  +
                    " 数量: " + order.getNums() +
                    " 单价: " + order.getPrice() + "元" +
                    " 优惠金额: " + order.getDiscount() + "元" +
                    " 总价: " + order.getTotalCost() + "元" +
                    " 订单已完成";
        }
    }

    /**
     * 商家接单
     * @return 商家是否成功接单
     */
    @Override
    public boolean take(){
        int i = 0;
        List<Order> orders = orderDAO.executeQuery(GET_ORDERS_FOR_MERCHANT_ID,loginUser.getId());
        List<Order> unOrders = new ArrayList<>();
        for (Order order: orders) {
            if (order.getState() == -1){
                unOrders.add(order);
            }
        }
        for (Order order: unOrders) {
            if (order.getState() == -1){
                i += orderDAO.executeUpdate(TAKE_ORDERS,order.getId());
            }
        }
        return unOrders.size() == i;
    }

    /**
     * 用户确认订单是否完成
     * @param order 订单
     * @return 订单是否完成
     */
    @Override
    public boolean over(Order order){
        try {
            Merchant merchant = merchantDAO.executeQuery(SELECT_MERCHANT_ASSET_BY_ORDERS_ID,order.getId()).get(0);
            Store store= storeDAO.executeQuery(SELECT_STORE_ASSET_BY_ORDERS_ID,order.getId()).get(0);
            orderDAO.executeUpdate(OVER_ORDERS,order.getId());
            merchantDAO.executeUpdate(UPDATE_MERCHANT_ASSET,merchant.getAsset() + order.getTotalCost(),merchant.getId());
            storeDAO.executeUpdate(UPDATE_STORE_ASSET,order.getTotalCost()+store.getAsset(),store.getId());
            return true;
        } catch (Exception e) {
            return false;
        }
    }


    /**
     * 获取选择
     * @param maxNum 选择最大值
     * @return 选择
     */
    @Override
    public int getChoose(int maxNum){
        while (true){
            try{
                String num;
                System.out.println("请输入要确定收货的订单序号(按0返回上一级):");
                num = SYS_SC.nextLine();
                while(Integer.parseInt(num) < 0 || Integer.parseInt(num) >= maxNum){
                    System.out.println("输入有误！请重新输入....");
                    num = SYS_SC.nextLine();
                }
                return Integer.parseInt(num);
            }catch (NumberFormatException e){
                System.out.println("输入有误！请重新输入....");
            }
        }
    }
}
